package com.gxa.clientproduct.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientproduct.pojo.Address;
import com.gxa.clientproduct.pojo.Grade;
import com.gxa.clientproduct.pojo.Sign;
import com.gxa.clientproduct.pojo.User;
import com.gxa.clientproduct.service.AddressService;
import com.gxa.clientproduct.service.GradeService;
import com.gxa.clientproduct.service.SignService;
import com.gxa.clientproduct.service.UserService;
import com.gxa.clientproduct.utils.JsonResult;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: fenglin
 * @Date:2020/11/11 14:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Api(value = "执行会员管理的控制器",tags = "会员管理模块")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    GradeService gradeService;

    @Resource
    SignService signService;
    @Resource
    AddressService addressService;
    JsonResult jsonResult=new JsonResult();


    @PostMapping("/findAll")
    @ApiOperation(value = "会员管理接口",notes = "查找会员的所有用户信息",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query")
    })
    public Map findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){

        Page<Object> startPage = PageHelper.startPage(page, limit);
        List<User> all = userService.findAll();
        PageInfo<User> userPageInfo = new PageInfo<>(all);
        jsonResult.put("count",userPageInfo.getTotal());
        jsonResult.setData(userPageInfo.getList());
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }


    @PostMapping("/findAddress")
    @ApiOperation(value = "会员管理接口",notes = "返回一个手机号查找用户信息地址",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "当条数据的手机号",required = true,dataType = "String",paramType = "query")
    })
    public Map findAddress(@RequestParam("phone") String phone){
        List<Address> address = addressService.findAddress(phone);
        jsonResult.setData(address);
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/gradeFindAll")
    public Map gradeFindAll(@RequestParam("page") int page, @RequestParam("limit") int limit){

        Page<Grade> startPage = PageHelper.startPage(page, limit);
        List<Grade> all = gradeService.gradeFindAll();
        PageInfo<Grade> userPageInfo = new PageInfo<>(all);
        jsonResult.put("count",userPageInfo.getTotal());
        jsonResult.setData(userPageInfo.getList());
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @RequestMapping("/updateGrade")
    @RequiresPermissions("23")
    @ApiOperation(value = "Vip等级",notes = "用于vip等级修改",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public  Map updateGrade(@RequestBody @ApiParam(name = "grade",value = "grade的pojo信息",required = true)Grade grade){

        int i = gradeService.updateGrade(grade);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return jsonResult.getValues();

    }

    @RequestMapping("/updateSign")
    @RequiresPermissions("24")
    @ApiOperation(value = "签到管理",notes = "用于签到的修改",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public  Map updateSign(@RequestBody @ApiParam(name = "sign",value = "sign的pojo信息",required = true)Sign sign){


        int i = signService.updateSign(sign);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return jsonResult.getValues();

    }


    @PostMapping("/findNameAndPhone")
    @ApiOperation(value = "会员管理接口",notes = "根据用户信息模糊查找名字和电话号",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "查找该手机号",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "查找该名称",required = true,dataType = "String",paramType = "query")
    })
    public Map findNameAndPhone(@RequestParam("page") int page, @RequestParam("limit") int limit,@RequestParam("phone") String phone,@RequestParam("name") String name){

        Page<Object> startPage = PageHelper.startPage(page, limit);
        List<User> nameAndPhone = userService.findNameAndPhone(name, phone);
        PageInfo<User> userPageInfo = new PageInfo<>(nameAndPhone);
        jsonResult.put("count",userPageInfo.getTotal());
        jsonResult.setData(userPageInfo.getList());
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }


    @PostMapping("/del")
    @ApiOperation(value = "会员管理删除接口",notes = "根据当条数据的手机号删除",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "根据当条数据的手机号删除",required = true,dataType = "String",paramType = "query")
    })
    public Map del(@RequestParam("phone") String phone){
        User user = new User();
        user.setPhone(phone);
        int i = userService.delete(user);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }

    @PostMapping("/updateAddress")
    public Map updateAddress(@RequestBody Address address){

        int i = addressService.updateAddress(address);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }
    @PostMapping("/insertAddress")
    public Map insertAddress(@RequestBody Address address){
        String phone = address.getPhone();
        List<Address> findAddress = addressService.findAddress(phone);
        int size = findAddress.size();
        System.out.println(size);

        if (size==0){
            addressService.insertAddress(address);
        }else {
            addressService.updateAddress(address);
        }
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }
    @PostMapping("/deleteAddress")
    public Map deleteAddress(@RequestParam String  phone){
        Address address = new Address();
        address.setPhone(phone);
        int i = addressService.deleteAddress(address);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }

    @PostMapping("/signIn")
    public Map signIn(@RequestParam String phone){

        Sign all = signService.findAll();
        Integer integral = all.getIntegral();

        User user = new User();
        User phone1 = userService.findPhone(phone);
        int score = phone1.getScore();
        user.setPhone(phone);
        System.out.println(integral+score);
        user.setScore(integral+score);
        userService.updateUser(user);
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }
}
